﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using QS.Drawables;
using Microsoft.Xna.Framework.Input;
using QS.Input;
using QS.Game.Logic.Objects;
using QS.Game.Logic.Physics;
using Microsoft.Xna.Framework.Content;

namespace QS.Logic.Objects
{    
    class QPhysicsObject : QObject, IDrawableObject
    {
        private QAbstractRenderer m_Renderer;

        private float m_Mass;
        private Vector3 m_Speed;
        private Vector3 m_Acceleration;
        private Vector3 m_Force;

        private List<QAbstractForce> m_AffectingForces;

        public QPhysicsObject()
        {
            Mass = 1.0f;
            Speed = Vector3.Zero;
            m_AffectingForces = new List<QAbstractForce>();
        }

        public List<QAbstractForce> AffectingForces
        {
            get
            {
                return this.m_AffectingForces;
            }
        }

        public float Mass
        {
            get { return m_Mass; }
            set { m_Mass = value; }
        }

        public Vector3 Speed
        {
            get { return m_Speed; }
            set { m_Speed = value; }
        }

        public Vector3 Acceleration
        {
            get { return m_Acceleration; }
            set { m_Acceleration = value; }
        }

        public Vector3 Force
        {
            get { return m_Force; }
            set { m_Force = value; }
        }

        public virtual void Update(GameTime time)
        {
            foreach (QAbstractForce force in m_AffectingForces)
                force.Update(time);

            UpdatePosition(time);
        }

        private void UpdatePosition(GameTime time)
        {
            Acceleration = Force / Mass;
            Speed += Acceleration * time.ElapsedGameTime.Milliseconds;
            Position += Speed;
            Force = Vector3.Zero;
        }

        public void Draw(GraphicsDevice device, QCamera camera, SpriteBatch spriteBatch)
        {
            foreach (QAbstractForce force in m_AffectingForces)
                force.Draw(device, camera, spriteBatch);

            this.Renderer.Draw(device, camera, spriteBatch, this);
        }

        public QAbstractRenderer Renderer
        {
            get
            {
                return this.m_Renderer;
            }

            set
            {
                this.m_Renderer = value;
            }
        }

        public virtual void Load(GraphicsDevice graphicsDevice, ContentManager contentManager)
        {

        }
    }
}
